package kr.hkit.powerjava.chapter16;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class KeyEventHandlerEx extends JFrame implements KeyListener {

	private JPanel contentPane;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					KeyEventHandlerEx frame = new KeyEventHandlerEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public KeyEventHandlerEx() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		textField = new JTextField();
		textField.addKeyListener(this);
		contentPane.add(textField, BorderLayout.CENTER);
		textField.setColumns(10);
	}

	public void keyPressed(KeyEvent e) {
		display(e, "Key Pressed");
	}
	public void keyReleased(KeyEvent e) {
		display(e, "Key Released");
	}
	public void keyTyped(KeyEvent e) {
		display(e, "Key Typed");
	}
	
	
	public void display(KeyEvent e, String s){
		char c = e.getKeyChar();
		int keyCode = e.getKeyCode();
		String info = e.isAltDown() + " : " + e.isControlDown() + " : " + e.isShiftDown();
		System.out.println(s + "\n" + c + "\n" + keyCode + "\n"+info);
	}
}
